﻿@model SIGCBI.Entities.DETALLE_PARTIDA

@{
    ViewBag.Title = "Asignar Partida a Area";
}

<link rel="stylesheet" href="~/Content/bootstrap.min.css">
<link rel="stylesheet" href="~/Content/themes/base/jquery-ui.css">
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-ui.js"></script>

<script>

</script>

<div id="dialog" class="panel panel-default">
    <div class="panel-heading">Búsqueda de Area</div>
    <div class="panel-body">
        <b>Criterio :  </b>@Html.TextBox("BuscarArea")
    </div>
    <div class="panel-footer"></div>
</div>
<button type="submit" id="opener" class="btn btn-default  btn-lg " value="Guardar">Buscar Area</button>


<script>

</script>


<script>

</script>

<button type="submit" id="openerP" class="btn btn-default  btn-lg " value="Guardar" style="position: absolute; left: 750px; top: 140px">Buscar Partida</button>

<div id="dialogP" class="panel panel-default">
    <div class="panel-heading">Búsqueda de Partida</div>
    <div class="panel-body">
        <b>Criterio :  </b>@Html.TextBox("BuscarPartida")
    </div>
    <div class="panel-footer"></div>
</div>


<script>

    
</script>


@{
    var message = TempData["AlertMessage"] ?? string.Empty;
}

<script type="text/javascript">
    var message = '@message';
    if (message)
        alert(message).alert;
</script>

<p></p>

@using (Html.BeginForm("AsignarPartidaArea", "Partida"))
{
    @Html.ValidationSummary(true)
    <div class="panel panel-default" style="position: inherit; left: 380px; top: 195px; width: 400px; height: 200px">
        <div class="panel-heading">Datos de Área</div>
        <div class="panel-body">
            <div class="table-responsive">
                <b>Area:</b><p></p>
                <input type="text" readonly="true" id="AreaSeleccionada" name="AreaSeleccionada" />
                @Html.Hidden("Id_Area")<p></p>
                <b>Encargado:</b><p></p>
                <input type="text" readonly="true" id="Encargado" name="Encargado" />
            </div>
        </div>
    </div>

    <div class="panel panel-default" style="position: absolute; left: 750px; top: 195px; width: 400px; height: 200px">
        <div class="panel-heading">Datos de Partida Presupuestaria</div>
        <div class="panel-body">
            <div class="table-responsive">
                <b>Nombre:</b> &nbsp;&nbsp;&nbsp;&nbsp;<input type="text" readonly="true" id="PartidaSeleccionada" name="PartidaSeleccionada" />
                @Html.Hidden("Id_Partida")
                <p></p>
                <p>
                    <b class="field-validation-error">Disponible:</b> &nbsp;&nbsp;&nbsp;&nbsp;<input type="text" readonly="true" id="saldo" name="saldo" class="field-validation-error" />
            </div>
        </div>
    </div>
        
    <div class="alert alert-warning" style="position: inherit; left: 930px; top: 495px; width: auto; height: auto">
        <b>Monto Asignado: </b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        @Html.EditorFor(model => model.MontoAsignado)
        @Html.ValidationMessageFor(model => model.MontoAsignado)
    </div>
    <p>
        <button type="submit" id="btnGuardar" class="btn btn-default btn-lg" value="Guardar">Guardar</button>
        <a href="@Url.Action("Index", "Home")" class="btn btn-default btn-lg active" role="button">Cancelar</a>
        <h3><span class="field-validation-error">@ViewBag.ResultMessage</span></h3>
    </p>
    <br />    
}


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">

        $(function () {
            $("#dialog").dialog({
                autoOpen: false,
                width: 450,
                height: 250,
                modal: true,
                show: {
                    effect: "toggle",
                    duration: 800
                },
                hide: {
                    effect: "toggle",
                    duration: 800
                }
            });

            $("#opener").click(function () {
                $("#dialog").dialog("open");
            });
        });

        $(function () {
            $("#dialogP").dialog({
                autoOpen: false,
                width: 450,
                height: 250,
                modal: true,
                show: {
                    effect: "toggle",
                    duration: 800
                },
                hide: {
                    effect: "toggle",
                    duration: 800
                }
            });

            $("#openerP").click(function () {
                $("#dialogP").dialog("open");
            });
        });


        $(function () {
            $("#BuscarArea").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: '@Url.Action("Search")', type: "POST", dataType: "json",
                        data: { nombreArea: request.term },
                        success: function (data) {
                            response($.map(data, function (item) {
                                return { label: item.label, value: item.id, encargado: item.encargado };
                            }));
                        }
                    });
                },
                select: function (event, ui) {
                    $("#BuscarArea").val("");
                    $("#Id_Area").val(ui.item.value);
                    alert("Ha seleccionado el Area: " + ui.item.label);
                    $("#AreaSeleccionada").val(ui.item.label);
                    $("#Encargado").val(ui.item.encargado);
                    $("#dialog").dialog("close");
                    return false;
                }
            });
        });

        $(function () {
            $("#BuscarPartida").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: '@Url.Action("SearchPartida")', type: "POST", dataType: "json",
                        data: { nombrePartida: request.term },
                        success: function (data) {
                            response($.map(data, function (item) {
                                return { label: item.label, value: item.id, saldo: item.saldo };
                            }));
                        }
                    });
                },
                select: function (event, ui) {
                    $("#BuscarPartida").val("");
                    $("#Id_Partida").val(ui.item.value);
                    alert("Ha seleccionado la Partida: " + ui.item.label);
                    $("#PartidaSeleccionada").val(ui.item.label);
                    $("#saldo").val(ui.item.saldo);
                    $("#dialogP").dialog("close");
                    return false;
                }
            });
        });


        //$("#btnGuardar").click(function () {
        //    var IdArea = $("#Id_Area").val();
        //    if (IdArea == 0) {
        //        alert("Debe seleccionar un Área");
        //        $("#dialog").dialog("open");
        //    }
        //});


        $("#MontoAsignado").focusout(function () {

            if ($(this).val() > 0) {

                var IdPartida = $("#Id_Partida").val();
                var div = $(this).closest("div");
                if (IdPartida > 0) {
                    $.ajax({
                        url: "/Partida/ValidarMonto?monto=" + monto + "&IdPartida=" + IdPartida,
                        dataType: "json",
                        success: function (data) {
                            if (data.correcto == false) {
                                $(div).find("spam").remove();
                                $(div).append('<spam class="field-validation-error"><br/>El monto asignado excede el monto disponible</spam>');
                                $(div).append('<spam class="field-validation-error"><br/>Monto Maximo Permitido: S/ ' + data.montoPermitido + '</spam>');
                                $("#btnGuardar").hide();
                            }
                            else {
                                $(div).find("spam").remove();
                                $("#btnGuardar").show();
                            }
                        }
                    });
                }
                else {
                    //alert("Debe seleccionar una Partida");
                    //$("#dialogP").dialog("open");
                    $(div).append('<spam class="field-validation-error"><br/>Debe seleccionar una Partida</spam>');
                }
            }

        });
    </script>
}




